<?php
/* ******************************
	Incoming Tasks aJax actions
   ******************************
*/
include('../../includes/classes/core.php');
$action 	= $_REQUEST['act'];
$data 		= '';
$error		= '';
$start		= $_REQUEST['start'];
$end		= $_REQUEST['end'];
$client_id	= $_REQUEST['id'];

switch ($action) {
	case 'get_list':
			$qq='';
			$count = $_REQUEST['count'];
			while ($end>=$start){
				$qq.=", sum(IF(MONTH(a_letter.fact_end_date)=$start,IF(NOT ISNULL(warehouse_detail.id),1,0),0)),
						sum(IF(MONTH(a_letter.fact_end_date)=$start,IF(warehouse_detail.type = 2, 0, cartridge_pricelist.price),0)),
    	    			sum(IF(MONTH(a_letter.fact_end_date)=$start,IF(NOT ISNULL(warehouse_detail.id),1,0),0))-sum(IF(MONTH(a_letter.job_end_date)= $start-1,IF(NOT ISNULL(warehouse_detail.id),1,0),0)),
						sum(IF(MONTH(a_letter.fact_end_date)=$start,IF(warehouse_detail.type = 2, 0, cartridge_pricelist.price),0)) - sum(IF(MONTH(a_letter.job_end_date)= $start-1 ,IF(warehouse_detail.type = 2, 0, cartridge_pricelist.price),0))";
				$start++;

			}
			//echo $qq; return 0;
				  	  $rResult = mysql_query("SELECT client.id,
									client.`name`
									$qq
									FROM client
									JOIN	client_objects ON client_objects.client_id = client.id
									JOIN	a_letter ON client_objects.id = a_letter.client_object_id
									JOIN   	warehouse ON warehouse.a_letter_id=a_letter.id
									JOIN   	warehouse_detail ON warehouse.id = warehouse_detail.warehouse_id AND warehouse_detail.actived=1
									JOIN   	warehouse_producing ON warehouse_producing.warehouse_detail_id = warehouse_detail.id
									JOIN  	cartridge_pricelist ON client.`id` = cartridge_pricelist.client_id AND warehouse_producing.cartridge_part_id = cartridge_pricelist.parts_type AND cartridge_pricelist.production_id = warehouse_detail.prod_id
									JOIN  	production ON SUBSTR(warehouse_detail.barcode, 6, 4) = production.id
									JOIN  	cartridge_parts_type ON cartridge_parts_type.id = warehouse_producing.cartridge_part_id
									WHERE   warehouse_producing.cartridge_part_id = 2
									GROUP BY client_objects.client_id");


		$data = array(
			"aaData"	=> array()
		);

		while ( $aRow = mysql_fetch_array( $rResult ) )
		{
			$row = array();
			for ( $i = 0 ; $i < $count ; $i++ )
			{
				$row[] = $aRow[$i];
			}
			$data['aaData'][] = $row;
		}
        break;

	case 'GetSums' :
		$page	= GetSums($start, $end);
		$data = array('sums'	=> $page);

		break;

    default:
       $error = "action null";
}

function GetSums($start, $end){
    	    $aRow = mysql_fetch_assoc(mysql_query("	SELECT      IF (ISNULL(SUM(part1.count)),0,SUM(part1.count))  AS `part1`,
    	    								  					IF (ISNULL(SUM(part2.count)),0,SUM(part2.count))  AS `part2`,
																IF (ISNULL(SUM(part3.count)),0,SUM(part3.count))  AS `part3`,
																IF (ISNULL(SUM(part4.count)),0,SUM(part4.count)) AS `part4`,
																IF (ISNULL(SUM(part5.count)),0,SUM(part5.count)) AS `part5`,
																IF(part1.count = 1, 0, SUM( IF(`warehouse_detail`.`type` = 2, 0, `cartridge_pricelist`.`price`))) AS sum,
																ROUND((SUM(`sale_transaction_detail`.`product_quantity` * `sale_transaction_detail`.`self_cost`)), 2) AS self,
																IF(part1.count = 1, 0, SUM( IF(`warehouse_detail`.`type` = 2, 0, `cartridge_pricelist`.`price`))) - ROUND((SUM(`sale_transaction_detail`.`product_quantity` * `sale_transaction_detail`.`self_cost`)), 2) AS diff
													FROM      	`warehouse_detail`
													LEFT JOIN   `warehouse` ON warehouse.id = warehouse_detail.warehouse_id
													LEFT JOIN   client ON SUBSTR(warehouse_detail.barcode, 2, 4) = client.id
													LEFT JOIN   warehouse_producing ON warehouse_detail.id = warehouse_producing.warehouse_detail_id
													LEFT JOIN   warehouse_producing AS `part1` ON warehouse_detail.id = part1.warehouse_detail_id AND part1.cartridge_part_id = 1
													LEFT JOIN   warehouse_producing AS `part2` ON warehouse_detail.id = part2.warehouse_detail_id AND part2.cartridge_part_id = 2
													LEFT JOIN   warehouse_producing AS `part3` ON warehouse_detail.id = part3.warehouse_detail_id AND part3.cartridge_part_id = 3
													LEFT JOIN   warehouse_producing AS `part4` ON warehouse_detail.id = part4.warehouse_detail_id AND part4.cartridge_part_id = 4
													LEFT JOIN   warehouse_producing AS `part5` ON warehouse_detail.id = part5.warehouse_detail_id AND part5.cartridge_part_id = 5
													LEFT JOIN   warehouse_producing AS `part6` ON warehouse_detail.id = part6.warehouse_detail_id AND part6.cartridge_part_id = 6
													LEFT JOIN   warehouse_producing AS `part7` ON warehouse_detail.id = part7.warehouse_detail_id AND part7.cartridge_part_id = 7
													LEFT JOIN   warehouse_producing AS `part8` ON warehouse_detail.id = part8.warehouse_detail_id AND part8.cartridge_part_id = 8
													LEFT JOIN   `sale_transaction_detail` ON `warehouse_producing`.`id` = `sale_transaction_detail`.`warehouse_producing_id`
													RIGHT JOIN  `cartridge_pricelist` ON `client`.`id` = `cartridge_pricelist`.`client_id` AND `warehouse_producing`.`cartridge_part_id` = `cartridge_pricelist`.`parts_type` AND `cartridge_pricelist`.`production_id` = `warehouse_detail`.`prod_id`
													WHERE       warehouse.`status` = 4 AND DATE(warehouse.start_date) BETWEEN '$start' AND '$end'") );

	$data = '

				<tr>
	        		<td>საგარანტიო</td>
	        		<td>' . $aRow['part1'] . ' ც</td>
	        		<td style="background:none !important; border:none !important;">&nbsp;</td>
	        		<td>სულ.რაოდენობა</td>
	        		<td>' . $aRow['sum'] .  ' ც</td>
	        	</tr>
	        	<tr>
	        		<td>ტონერი</td>
	        		<td>' . $aRow['part2'] . ' ც</td>
	        		<td style="background:none !important; border:none !important;">&nbsp; &nbsp;</td>
	        		<td>სულ თვითღირებულება</td>
	        		<td>' . $aRow['self'] .  ' ლ</td>
	        	</tr>
	        	<tr>
	        		<td>ბარაბანი</td>
	        		<td>' . $aRow['part3'] . ' ც</td>
	        		<td style="background:none !important; border:none !important;">&nbsp;</td>
	        		<td>დ. მოგება</td>
	        		<td>' . $aRow[diff] .  ' ლ</td>
	        	</tr>
	        	<tr>
	        		<td>მაგნ. ლილვი</td>
	        		<td>' . $aRow['part4'] . ' ც</td>
	        	</tr>
	        	<tr>
	        	<td>მაგნ. ლილვი</td>
	        		<td>' . $aRow['part5'] . ' ც</td>
	        	</tr>
		    ';
	return $data;
}


$data['error'] = $error;

echo json_encode($data);

?>